package manager
{
	import Classes.pi_events;
	
	import UIFree.ObjectEvent;
	
	import api_servers.api_mailserver;
	import api_servers.api_myserver;
	
	import cardsbody.pi_pngcard;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	import mailru.MailruCall;
	import mailru.MailruCallEvent;
	
	public class pi_mailManager extends pi_manager {
		private var _payObject	: Object;
		public static const MAIL_ERRORS: Object = {'FRIEND_QUERYID':'Ошибка при загрузке списка друзей'};
		
		public function pi_mailManager(a_app: photo_inject) {
			super(a_app);
			app.addEventListener(api_mailserver.MAIL_ERROR, doMailError);
			app.addEventListener(api_myserver.MYERROR, doMailError);
			app.addEventListener(pi_events.PAYFROMMAILIKI, doPayMailiki);
			MailruCall.addEventListener(MailruCallEvent.INCOMING_PAYMENT, doPayState);
		} 		

		override protected function errorAlert(error: Object): void {
			if (MAIL_ERRORS[error.queryID] > '') { 
				app.alert(pi_data.WARNING, MAIL_ERRORS[error.queryID] + "\n" + pi_data.REBOOT_INSTRUCTION);
			} else super.errorAlert(error);
		}
		
		protected function doMailError(e: GDataEvent): void {
			errorAlert(e.data);
		}
		
		override protected function doStartPayment(e: ObjectEvent): void {
			_payObject = e.object;
			
			var obj: Object = {
				service_id 			: 2,
//				service_name		: 'Пополнение баланса',
				other_price 		: e.object.other_price * 100,
				sms_price 			: e.object.sms_price,
				service_name		: ' на ' + e.object.other_price + ' руб.'
			}
			
			MailruCall.exec('mailru.app.payments.showDialog', null, obj);
		}
		
		protected function doPayMailiki(e: ObjectEvent): void {
			var obj: Object = {
				service_id 			: 2,
//				service_name		: 'Пополнение баланса',
				mailiki_price 		: e.object.value,
				service_name		: e.object.value + ' мэйликов'
			}
			
			MailruCall.exec('mailru.app.payments.showDialog', null, obj);
		}
		
		override protected function doInviteDialog(e: Event): void {
			function doInvite(e: MailruCallEvent=null): void {
				if (e.data.status == 'closed') {
					MailruCall.removeEventListener('app.friendsInvitation', doInvite);
//					if (e.data.hasOwnProperty('data') && (e.data.data.length > 0))
						app.alert(app.classes.data.MESSAGE, app.siteData.INVITEDETAIL);
				}
			}
			MailruCall.addEventListener('app.friendsInvitation', doInvite);
			MailruCall.exec('mailru.app.friends.invite');
		}
		
		// Вариант использования JS API payments.showDialog

		protected function doPayState(e: Event): void {
			function doNextCheckBalance(result: Boolean): void {
				if (!result) app.dispatchEvent(new Event(pi_events.PAYBAD));
				else if (_payObject && _payObject.postPay) _payObject.postPay()
				_payObject = null; 
			}
			checkChangeBalance(doNextCheckBalance);
		}
		
		override protected function afterSend(users: Array, data: Object, textPublish: String, link: String): void {
			var send_id: String = data.send_id;
			var publishRec: Object =  {
//				uid		: users[0].uid,
				title	: pi_data.USERSENDMESSAGE,
				text	: textPublish,
				img_url	: pi_data.infoFromID(app.cardbody.maskInfo.id).preview,
				action_links: [
					{
						text: pi_data.USERSENDCARDVIEW,
						href: 'state=sid,' + send_id
					}
				]
			}
			
			app.alert(app.classes.data.MESSAGE, app.classes.data.SENDOK);
			if (app.cardbody is pi_pngcard) {
				collectJPG(app.cardbody['asBytes'](new Point(75, 110)), function(fileName: String): void {
					function doPublish(e: MailruCallEvent): void {
						if (e.data.status != 'opened') {
							MailruCall.removeEventListener('common.streamPublish', doPublish);
							if (e.data.status != 'publishSuccess') app.myserver.removeImage(fileName);
						}
					}
					
					publishRec.img_url = fileName;
					MailruCall.addEventListener('common.streamPublish', doPublish);
					MailruCall.exec('mailru.common.stream.post', null, publishRec);
				}, 'from_publish/sid_' + send_id + '.jpg', false, false);
			} else {
				MailruCall.exec('mailru.common.stream.post', null, publishRec);
			}
		}
	}
}